#include "clientFunc.h"

int main(int argc, char *argv[])
{
    // ./client ip port
    ARGS_CHECK(argc, 3);
    // 变量的定义
    char *command = NULL;    // 存储命令
    char *commandArg = NULL; // 存储命令的参数
    train_t train;           // 小火车
    char fullcmd[256] = {0}; // 存储命令行
    // 存储服务端的IP和端口号并建立连接
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    ERROR_CHECK(ret, -1, "connect");
 
  //------------------------------------------------------fld---------------------------------
    while(1){
    char username[1024] = {0};
    char *password;
    printf("请输入用户名\n");
     scanf(" %s",username);
     //去除最后的\n
     username[strlen(username)] = '\0'; //warning ..@
    password = getpass("Input a password");
    //发送用户名的长度
    int len = strlen(username);
    write(sockfd,&len,sizeof(int));
    //发送用户名
    write(sockfd,username,len);
    int success = 1;
    //
    recvn(sockfd,&success,sizeof(int));
    if( success < 0 ){
        printf("没有这个用户\n");
        continue;
    }
    //接受一个盐值
     char salt[512]={0};
     //这里没有用小火车
     recvn(sockfd,&salt,12);
    //合并一个密文
    char* f_psd = crypt(password,salt);
    //发送密文长度
    len = strlen(f_psd);
    write(sockfd,&len,sizeof(len));
    write(sockfd,f_psd,len);
    //接受一条放回消息
    char msg[513];
    len = 0;
    recvn(sockfd,&len,sizeof(int));
    recvn(sockfd,msg,len);
    if(len > 513){
        printf("接受到诡异的东西了\n");
    }
    if(strcmp(msg,"SUCCESS") == 0){
        printf("登录成功！\n");
         break;
    }else{
        printf("您的密码或者用户不正确\n");
    }
    }
    //-------------可以建立一个新的函数-------------------------fld---------------
 
 
 
    // 循环从标准输入中读取命令行
    while (bzero(fullcmd, sizeof(fullcmd)), read(0, fullcmd, sizeof(fullcmd)) > 0)
    {
        // 将命令行拆分为命令和参数并分别发送给服务端
        command = strtok(fullcmd, " ");
        train.length = sizeof(train.data);
        memcpy(train.data, command, train.length);
        send(sockfd, &train, sizeof(int) + train.length, MSG_NOSIGNAL);
        commandArg = strtok(NULL, " ");
        train.length = sizeof(train.data);
        memcpy(train.data, commandArg, train.length);
        send(sockfd, &train, sizeof(int) + train.length, MSG_NOSIGNAL);
        // 根据不同的命令执行对应的操作
        if (strncmp("cd", command, 2) == 0) // 如果是cd，直接接收服务端的内容并打印
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            printf("The current path :%s\n", train.data);
        }
        else if (strncmp("ls", command, 3) == 0) // 如果是ls,直接接收服务端的内容并打印
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            print_by_bytes(train.data, train.length);
        }
        else if (strncmp("puts", command, 4) == 0)
        {
            int fileNameLength = strlen(commandArg); // 如果命令是puts,参数就是文件名
            transFile(sockfd, commandArg, fileNameLength);
        }
        else if (strncmp("gets", command, 4) == 0)
        {
            recvFile(sockfd);
        }
        else if (strncmp("remove", command, 6) == 0)
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            print_by_bytes(train.data, train.length);
        }
        else if (strncmp("pwd", command, 3) == 0)
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            printf("The current path :");
            print_by_bytes(train.data, train.length);
        }
        else if (strncmp("mkdir", command, 5) == 0)
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            print_by_bytes(train.data, train.length);
        }
        else if (strncmp("rmdir", command, 5) == 0)
        {
            recvn(sockfd, &train.length, sizeof(int));
            recvn(sockfd, train.data, train.length);
            print_by_bytes(train.data, train.length);
        }
        else
        {
            continue;
        }
    }
    // TODO 退出
    return 0;
}
